package com.baymax.test.scene.demo

import android.content.Context
import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.util.TypedValue
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.Adapter
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.newScrollContainer
import com.bytedance.scene.Scene
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class LayoutManagerScene : BaseScene() {
    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {
        return p1.newScrollContainer {
            val scene = this@LayoutManagerScene
            val context = scene.requireSceneContext()
            val case = StartPx160(scene, this)
            case.plug()

            val case2 = VerticalCenter(scene, this, 400, 600)
            case2.plug()
        }
    }


    abstract class RVCase @JvmOverloads constructor(
        val scene: Scene,
        val parent: ViewGroup,
        val width: Int = ViewGroup.LayoutParams.MATCH_PARENT,
        val height: Int = 100
    ) {


        val rv: RecyclerView = RecyclerView(scene.requireSceneContext())


        init {
            rv.layoutManager = LinearLayoutManager(
                scene.requireSceneContext(),
                LinearLayoutManager.HORIZONTAL,
                false
            )
            rv.addItemDecoration(object : RecyclerView.ItemDecoration() {
                override fun getItemOffsets(
                    outRect: Rect,
                    view: View,
                    parent: RecyclerView,
                    state: RecyclerView.State
                ) {
                    super.getItemOffsets(outRect, view, parent, state)
                    outRect.right = 20
                    outRect.bottom = 20
                }
            })
            parent.addView(rv, MarginLayoutParams(width, height).apply {
                marginStart = 60
                marginEnd = 60
            })
            parent.background = GradientDrawable().apply {
                setStroke(1, Color.BLUE)
            }
        }


        class VH(context: Context) : RecyclerView.ViewHolder(TextView(context)),
            View.OnFocusChangeListener, CoroutineScope by MainScope() {
            init {
                val text = itemView as TextView
                text.setText(this.absoluteAdapterPosition.toString())
                text.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14f)
                text.setTextColor(Color.WHITE)
                text.minWidth = 80
                text.minHeight = 80
                text.gravity = Gravity.CENTER
                text.background = newBg(Color.BLUE)
                text.isFocusable = true
                text.onFocusChangeListener = this
            }

            fun newBg(
                color: Int,
                bord: Int = 2,
                borderColor: Int = Color.YELLOW,
                radius: Int = 8
            ): GradientDrawable {
                return GradientDrawable().apply {
                    setColor(color)
                    setStroke(bord, borderColor)
                    cornerRadius = radius.toFloat()
                }
            }

            override fun onFocusChange(v: View, hasFocus: Boolean) {
                v.background = if (hasFocus) newBg(Color.RED) else newBg(Color.BLUE)
                if (hasFocus) {

                    v.animate().scaleX(1.1f).scaleY(2f).setDuration(300).start()
                } else {
                    v.animate().scaleY(1f).scaleX(1f).start()
                }
                println(this.bindingAdapterPosition)
                if (hasFocus) {
                    launch {
                        while (true){
                            delay(100)


                        }
                    }
                } else {
                    v.layoutParams.height.takeIf { it  != 80 }?.let {
                        v.layoutParams.height = 80
                        v.requestLayout()
                    }
                    cancel()
                }
            }

            fun bind(position: Int) {
                val textView = itemView as TextView
                textView.setText(position.toString())
            }
        }


        class VHText(context: Context) : RecyclerView.ViewHolder(TextView(context)),
            View.OnFocusChangeListener, CoroutineScope by MainScope() {
            init {
                val text = itemView as TextView
                text.setText(this.absoluteAdapterPosition.toString())
                text.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14f)
                text.setTextColor(Color.WHITE)
                text.minWidth = 80
                text.minHeight = 80
                text.gravity = Gravity.CENTER
                text.background = newBg(Color.BLUE)
                text.isFocusable = true
                text.onFocusChangeListener = this
            }

            fun newBg(
                color: Int,
                bord: Int = 2,
                borderColor: Int = Color.YELLOW,
                radius: Int = 8
            ): GradientDrawable {
                return GradientDrawable().apply {
                    setColor(color)
                    setStroke(bord, borderColor)
                    cornerRadius = radius.toFloat()
                }
            }

            override fun onFocusChange(v: View, hasFocus: Boolean) {
                v.background = if (hasFocus) newBg(Color.RED) else newBg(Color.BLUE)
                if (hasFocus) {

                    v.animate().scaleX(1.1f).scaleY(2f).setDuration(300).start()
                } else {
                    v.animate().scaleY(1f).scaleX(1f).start()
                }
                println(this.bindingAdapterPosition)
                if (hasFocus) {
                    launch {
                        while (true){
                            delay(100)


                        }
                    }
                } else {
                    v.layoutParams.height.takeIf { it  != 80 }?.let {
                        v.layoutParams.height = 80
                        v.requestLayout()
                    }
                    cancel()
                }
            }

            fun bind(position: Int) {
                val textView = itemView as TextView
                textView.setText(position.toString())
            }
        }


        fun simpleHolder(context: Context): RecyclerView.ViewHolder {
            return VH(context)
        }

        fun newAdapter(childCount: Int): Adapter<VH> {
            return object : Adapter<VH>() {
                override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
                    return VH(parent.context)
                }

                override fun getItemCount(): Int {
                    return childCount
                }

                override fun onBindViewHolder(holder: VH, position: Int) {
                    holder.bind(position)
                }

            }
        }

        abstract fun plug()
    }

    class StartPx160(scene: Scene, parent: ViewGroup) : RVCase(scene, parent) {
        init {
            rv.layoutManager =
                object :
                    LinearLayoutManager(scene.sceneContext, LinearLayoutManager.HORIZONTAL, false) {
                    override fun requestChildRectangleOnScreen(
                        parent: RecyclerView,
                        child: View,
                        rect: Rect,
                        immediate: Boolean,
                        focusedChildVisible: Boolean
                    ): Boolean {
                        val centerX = child.left - 80
                        parent.smoothScrollBy(centerX, 0)
                        return false
//                        rect.left =  - centerX
//                        rect.right =  parent.right -  centerX
//8                        return super.requestChildRectangleOnScreen(parent, child, rect, immediate, focusedChildVisible)
                    }
                }

        }

        override fun plug() {
            rv.adapter = newAdapter(100)
        }
    }
}